package com.kede.app.rest;

import com.kede.annotation.AnonymousAccess;
import com.kede.annotation.Log;
import com.kede.app.domain.SysArea;
import com.kede.app.service.SysAreaService;
import com.kede.app.service.dto.SysAreaQueryCriteria;
import com.kede.app.service.impl.vo.AreaInfoVo;
import com.kede.app.service.impl.vo.JsDict;
import com.kede.pojo.ResHttp;
import com.kede.rest.po.ResourceAreaMapPo;
import com.kede.service.ResourceAreaMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author Pr.li
* @date 2023-04-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "area管理")
@RequestMapping("/api/sysArea")
public class SysAreaController {

    private final SysAreaService sysAreaService;

    private final ResourceAreaMapService resourceAreaMapService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysArea:list')")
    public void exportSysArea(HttpServletResponse response, SysAreaQueryCriteria criteria) throws IOException {
        sysAreaService.download(sysAreaService.queryAll(criteria), response);
    }

    /**
    * 查询area
    **/
    @GetMapping
    @Log("查询area")
    @ApiOperation("查询area")
    @PreAuthorize("@el.check('sysArea:list')")
    public ResponseEntity<Object> querySysArea(SysAreaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sysAreaService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增area
    **/
    @PostMapping
    @Log("新增area")
    @ApiOperation("新增area")
    @PreAuthorize("@el.check('sysArea:add')")
    public ResponseEntity<Object> createSysArea(@Validated @RequestBody SysArea resources){
        return new ResponseEntity<>(sysAreaService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改area
    **/
    @PutMapping
    @Log("修改area")
    @ApiOperation("修改area")
    @PreAuthorize("@el.check('sysArea:edit')")
    public ResponseEntity<Object> updateSysArea(@Validated @RequestBody SysArea resources){
        sysAreaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除area
    **/
    @DeleteMapping
    @Log("删除area")
    @ApiOperation("删除area")
    @PreAuthorize("@el.check('sysArea:del')")
    public ResponseEntity<Object> deleteSysArea(@RequestBody Integer[] ids) {
        sysAreaService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 根据地区编码获取详细的 省市区地址
     * @param code
     * @return
     */
    @AnonymousAccess
    @GetMapping("/getDetailAreaByAdcode")
    public ResHttp getDetailAreaByAdcode(Integer code){
        AreaInfoVo detailByAdcode = sysAreaService.findDetailByAdcode(code);
        return ResHttp.okData(detailByAdcode);
    }

    /**
     * 获取json格式的地区数据
     * @param adcode
     * @return
     */
    @AnonymousAccess
    @GetMapping("getJSAreaDicData")
    public ResHttp getTest(Integer adcode){

        List<JsDict> jsDicts = sysAreaService.formatAreatoJS(adcode);
        return ResHttp.okData(jsDicts);
    }

    @GetMapping("getAreaByResourceIds")
    public ResHttp getAreaByResourceIds(ResourceAreaMapPo resourceAreaMapPo){
        List<Integer> areaIdsByResourceIds = resourceAreaMapService.getAreaIdsByResourceIds(resourceAreaMapPo.getResourceIds());
        return ResHttp.okData(areaIdsByResourceIds);
    }


}